package chapter07

object Test17_CommonWordCount {
  def main(args: Array[String]): Unit = {
    val stringList = List(
      "hello",
      "hello world",
      "hello scala",
      "hello spark from scala",
      "hello flink from scala"
    )
    // 1.对字符串进行切分,得到一个打散所有单词的列表
    //    val worldList1: List[Array[String]] = stringList.map(_.split(" "))
    //    val worldList2: List[String] = worldList1.flatten
    val worldList2: List[String] = stringList.flatMap(_.split(" "))
    println(worldList2)

    //2.将相同的单词进行分组
    val groupMap: Map[String, List[String]] = worldList2.groupBy(word => word)
    //3.对分组之后的List取长度，得到每个单词的个数
    val countMap: Map[String, Int] = groupMap.map(kv => (kv._1, kv._2.length))
    //4.将map转换为list,并排序取前3
    val sortList: List[(String, Int)] = countMap.toList.sortWith(_._2 > _._2).take(3)
    println(sortList)
  }

}
